<?php 
class Controller extends SmartyView{
	//存放变量内容
	private $var = array();
	/**
	 * 构造方法
	 * [__construct description]
	 */
	public function __construct(){
		if(C("SMARTY_ON")){
			parent::__construct();
		}
		if(method_exists($this, '__init')){
			$this->__init();
		}

		if(method_exists($this, '__auto')){
			$this->__auto();
		}
	}

	/**
	 * [get_tpl 组合模板]
	 * @param  [type] $tpl [description]
	 * @return [type]      [description]
	 */
	protected function get_tpl($tpl = NULL){
		if(is_null($tpl)){
			$path = APP_TPL_PATH ."/".CONTROLLER."/".ACTION.".html";
		}else{
			$suffix = strrchr($tpl,".");
			$tpl = empty($suffix) ? $tpl."html" : $tpl;
			$path = APP_TPL_PATH."/".CONTROLLER."/".$tpl;
		}
		return $path;
	}

	/**
	 * [display 加载模版]
	 * @param  [type] $tpl [文件名称]
	 * @return [type]      [description]
	 */
	protected function display($tpl = NULL){
		$path = $this->get_tpl($tpl);
		if(!file_exists($path)) halt($path."模版文件不存在");
		if(C("SMARTY_ON")){  //调用smarty
			parent::display($path); 
		}else{
			extract($this->var);
			include $path;
		}
	}

	/**
	 * [assign 分配变量]
	 * @param  [type] $var   [key]
	 * @param  [type] $value [值]
	 * @return [type]        [description]
	 */
	protected function assign($var,$value){
		if(C("SMARTY_ON")){  //调用smarty
			parent::assign($var,$value); 
		}else{
			$this->var[$var] = $value;
		}
	}

	/**
	 * [success 成功提示方法]
	 * @param  [type]  $msg  [description]
	 * @param  [type]  $url  [description]
	 * @param  integer $time [description]
	 * @return [type]        [description]
	 */
	protected function success($msg,$url = NULL,$time = 3){
		$url = $url ? "window.location.href='".$url."'" : "window.history.back(-1)";
		include APP_TPL_PATH.'/success.html';
	}

	/**
	 * [error 错误提示方法]
	 * @param  [type]  $msg  [description]
	 * @param  [type]  $url  [description]
	 * @param  integer $time [description]
	 * @return [type]        [description]
	 */
	protected function error($msg,$url = NULL,$time = 3){
		$url = $url ? "window.location.href='".$url."'" : "window.history.back(-1)";
		include APP_TPL_PATH.'/error.html';
	}
}
?>